package com.prs.crm.action.sale;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.sale.InvoiceType;
import com.prs.crm.service.sale.InvoiceTypeService;
import com.prs.crm.service.sys.LogService;

public class InvoiceTypeAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;

	private InvoiceType invoiceType;

	private Integer[] typeIds;

	private InvoiceTypeService invoiceTypeService;

	private PaginationSupport pagination;
	
	private LogService logService;
	private String invoiceTypeName;

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public String getInvoiceTypeName() {
		return invoiceTypeName;
	}

	public void setInvoiceTypeName(String invoiceTypeName) {
		this.invoiceTypeName = invoiceTypeName;
	}

	@Action("listInvoiceType")
	public String listInvoiceType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				InvoiceType.class);
		this.setPagination(this.getInvoiceTypeService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editInvoiceType")
	public String editInvoiceType() {
		return SUCCESS;
	}

	@Action(value = "saveInvoiceType", results = { @Result(name = "success", type = "redirect", location = "listInvoiceType"),
			@Result(name="error",type="chain",location="editInvoiceType")})
	public String saveInvoiceType() {
		if(invoiceType.getName() == null || invoiceType.getName().equals("")) {
			this.addActionError("发票类型名称不能为空");
			return ERROR;
		}
		if(this.getLogService().getSearchName(InvoiceType.class, invoiceType.getName())){
			if(!(invoiceTypeName.trim().equals(invoiceType.getName().trim()))){
				this.addActionError("名称已存在");
				return ERROR;
			}
		}
		this.getInvoiceTypeService().save(invoiceType);
		return SUCCESS;
	}

	@Action(value = "removeInvoiceType", results = { @Result(name = "success", type = "redirect", location = "listInvoiceType") })
	public String removeInvoiceType() {
		this.getInvoiceTypeService().batchRemove(typeIds);
		return SUCCESS;
	}

	
	public Integer[] getTypeIds() {
		return typeIds;
	}

	public void setTypeIds(Integer[] typeIds) {
		this.typeIds = typeIds;
	}


	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public InvoiceType getInvoiceType() {
		return invoiceType;
	}

	public void setInvoiceType(InvoiceType invoiceType) {
		this.invoiceType = invoiceType;
	}

	public InvoiceTypeService getInvoiceTypeService() {
		return invoiceTypeService;
	}

	public void setInvoiceTypeService(InvoiceTypeService invoiceTypeService) {
		this.invoiceTypeService = invoiceTypeService;
	}

	
}
